package com.mls.fivedistrict.controller.backend;


import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.NewsComment;
import com.mls.fivedistrict.server.NewsCommentService;
import com.mls.fivedistrict.server.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController("backendNewsCommentController")
@RequestMapping("/api/manager")
public class NewsCommentController extends BaseController {
    @Autowired
    NewsCommentService newsCommentService;
    @Autowired
    NewsService newsService;


    /**
     * 删除评论
     *
     * @param newsCommentId
     * @return
     */
    @DeleteMapping("/v1/newsComment/{newsCommentId}")
    public ServiceResponse deleteNewsComment(@PathVariable Long newsCommentId) {
        Optional.ofNullable(newsCommentId).orElseThrow(() -> new CustomerException("评论内容id不能为空"));
        NewsComment newsComment = newsCommentService.selectById(newsCommentId);
        Optional.ofNullable(newsComment).orElseThrow(() -> new CustomerException("查无此评论"));
        newsComment.setDelete(true);
        boolean delete = newsCommentService.updateById(newsComment);
        Long newsId = newsComment.getNewsId();
        boolean update = newsService.cutCommentTimes(newsId);
        boolean flag=delete&&update;
        if (flag) {
            return ServiceResponse.createSuccessResponse("删除评论成功");
        } else {
            if(update){
                newsService.addCommentTimes(newsId);
            }
            return ServiceResponse.createErrorResponse("删除评论失败");
        }
    }

    /**
     * 修改评论
     *
     * @param newsCommentId
     * @param newsComment
     * @return
     */
    @PutMapping("/v1/newsComment/{newsCommentId}")
    public ServiceResponse updateNewsComment(@PathVariable Long newsCommentId, @RequestBody NewsComment newsComment) {
        Optional.ofNullable(newsCommentId).orElseThrow(() -> new CustomerException("评论内容id不能为空"));
        Optional.ofNullable(newsComment).orElseThrow(() -> new CustomerException("内容不能为空"));
        newsComment.setId(newsCommentId);
        boolean update = newsCommentService.updateById(newsComment);
        if (update) {
            return ServiceResponse.createSuccessResponse("修改成功");
        } else {
            return ServiceResponse.createErrorResponse("修改失败");
        }
    }

    /**
     * 查询单个评论接口
     * @param newsCommentId
     * @return
     */
    @GetMapping("/v1/newsComment/{newsCommentId}")
    public ServiceResponse getNewsComment(@PathVariable Long newsCommentId){
        Optional.ofNullable(newsCommentId).orElseThrow(() -> new CustomerException("评论内容id不能为空"));
        NewsComment newsComment = newsCommentService.selectById(newsCommentId);
        if(null!=newsComment){
            return ServiceResponse.createSuccessResponse(newsComment);
        }else{
            return ServiceResponse.createErrorResponse("查无此评论");
        }
    }

    /**
     * 评论列表
     * @return
     */
    @GetMapping("/v1/newsComments/{newsId}")
    public ServiceResponse getNewsCommentList(@PathVariable Long newsId){
        PageFilter pageFilter = RequestHolder.getPageFilter();
        PageHelper.startPage(pageFilter.getPageNum(),pageFilter.getPageSize());
        Wrapper wrapper = pageFilter.getWrapper();
        if(null==wrapper){
            wrapper=Condition.create();
        }
        wrapper.eq("news_id",newsId);
        List list = newsCommentService.selectList(wrapper);
        PageInfo pageInfo = new PageInfo<NewsComment>(list, 5);
        return  ServiceResponse.createSuccessResponse(pageInfo);
    }

}
